﻿#define VERBOSE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using Device;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;

namespace ConsoleApp
{
    internal class Program
    {
#if DEBUG
        //static void Main(string[] args)
        //{
        //    var iHostBuilder = Host.CreateDefaultBuilder(args);
        //    iHostBuilder.ConfigureServices((services) => ConfigureServices(services));
        //    //iHostBuilder.ConfigureServices((webBuilder) => ConfigureServices(services));
        //    iHostBuilder.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
        //    var host = iHostBuilder.Build();
        //    host.RunAsync();
        //    Console.ReadLine();
        //    Console.WriteLine("Hello World!");
        //}
#endif

#if VERBOSE
        static void Main(string[] args)
        {
            Global.bServerId = StringHexByteUtils.StringIpToBytes("192.168.30.163");
            UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("192.168.30.163"), 8070)) { EnableBroadcast = true };
            var bMsg = ServerProtocolEncode.GenHandShake(Global.bServerId, null);
            var sMsg = StringHexByteUtils.ByteToHexString(bMsg);
            udpClient.Send(bMsg, bMsg.Length, new IPEndPoint(IPAddress.Parse("192.168.30.255"), 8070));
            Task.Run(() => ReceiveData(udpClient));
            Console.ReadLine();
        }
        private static List<string> anchoridDic = new List<string>(); 
        private static void ReceiveData(UdpClient udpClient)
        {
            while (true)
            {
                IPEndPoint remoteIpep = null;
                byte[] buffer = udpClient.Receive(ref remoteIpep);
                //Console.WriteLine($"{remoteIpep.Address.ToString()}:{remoteIpep.Port}");
                ProcessAnchorHandshake(buffer);
            }
        }
        private static void ProcessAnchorHandshake(byte[] e)
        {
            var bAnchorid = ServerProtocolDecode.GetAnchorId(e.Skip(0).Take(e.Length).ToArray());
            var anchorid = StringHexByteUtils.ByteToHexString(bAnchorid).Replace(" ", "");
            if (!anchoridDic.Contains(anchorid))
            {
                anchoridDic.Add(anchorid);
            }
            Console.WriteLine(anchorid);
            Console.WriteLine(StringHexByteUtils.ByteToHexString(e.Skip(0).Take(e.Length).ToArray()));

            //if (!anchorDict.Keys.Contains(anchorid))
            //{
            //    //var anchor = new Anchor();
            //    //anchor.AnchorId = anchorid;
            //    //anchor.IP = ((IPEndPoint)e.Ep).Address.ToString();
            //    //anchor.RunningStatus = "Online";
            //    //anchorDict[anchorid] = anchor;
            //    //await anchorService.Insert(anchor);
            //}
            //anchorDict[anchorid].RunningStatus = "Online";
            //anchorDict[anchorid].IP = ((IPEndPoint)e.Ep).Address.ToString();  
            //NotifyEventArgs arg = new NotifyEventArgs();
            //arg.NotifyBody = Encoding.UTF8.GetBytes(anchorid);
            //arg.EventType = "HandshakeResult";
            //OnDeviceNotify(this, arg);
            //SendHeartBeatWithList();
        }
        private static void ConfigureServices(IServiceCollection services)
        {
            //services.AddHostedService<Worker>();
        }
#endif
    }
}
